<%@page import="cn.jekun.component.base.exceptions.BusinessException"%>
<%@page import="net.sf.json.JSONObject"%>
<%@page import="cn.jekun.component.base.spring.SpringUtils"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="cn.jekun.shop.app.service.LoginService"%>
<%@ page import="java.util.*"%>
<%@ page import="net.sf.json.JSONObject"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<head>
<!-- Basic Page Needs
================================================== -->
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no"/>  
<title>发放优惠</title>
<!-- Mobile Specific
================================================== -->
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1" />
<!-- CSS
================================================== -->
<link rel="stylesheet" type="text/css" href="css/style.css" />
<script src="js/jquery-1.7.1.min.js"></script>
<script src="js/basic.js"></script>
<%
	String mobile = request.getParameter("mobile"); //操作员登录名
	String password = request.getParameter("password"); //登录密码
	String userId = request.getParameter("userId"); //已登录的用户代码
%>
<script type="text/javascript">
function goNext(userId){
	var phoneNum = document.getElementById("phoneNum").value;
	if(phoneNum==""){
		return;
	}else if(phoneNum == '<%=mobile%>'){
		alert("工作人员禁止领优惠");
		return;
	}else{
		window.location="redEnvelopes.jsp?mobile=${param.mobile}&userId="+userId+"&phoneNum="+phoneNum;
	}
}
</script>
</head>
<body class="">
	
	<%--头部 --%>
	<div class="header clearfix">
		<a href="login.jsp?mobile=<%=mobile==null?"":mobile%>"></a><span>操作员:<%=mobile==null?"":mobile%></span>
	</div>

<%
	if(userId == null && ( mobile==null || mobile.trim().length()==0 || password == null || password.trim().length()==0) ){
%>
	<p style="color: red;">请输入工作人员用户名和密码</p>
<%
		return;
	}
	
	String message = null;
	if(userId==null){//还没有登录
		//System.out.println("mobile = " + mobile + " &password = " + password);
		LoginService loginService = SpringUtils.getBean(LoginService.BEAN_NAME); //获取登录实例
		JSONObject jsonObject = null;
		try{
			jsonObject = loginService.loginApp(mobile, password);
			if(jsonObject == null){
				message = "登录错误";
			}else{
				if ("1".equals(jsonObject.getString("code"))) {//登录成功
					userId = jsonObject.get("userId").toString();
					session.setAttribute("_RED_ENVELOPERS_LOGIN_", "1");//设置登录成功标记
				}else{
					message = jsonObject.getString("message");
				}
			}
		}catch(BusinessException e){
			message=e.getMessage();
		}
		
		//System.out.println("jsonObject = " + jsonObject);
	}
%>


	
	<%
		if (userId !=null) {//已经登录
			//判断是否已经登录
			if(!"1".equals( (String)session.getAttribute("_RED_ENVELOPERS_LOGIN_") ) ){
				 out.print("<a style='color: red;' href='login.jsp?mobile='"+(mobile==null?"":mobile)+"'>超时或未登录，点这里请重新登录。</a>");
				 return;
			}
	%>
	<div class="btnBox">
		<div class="cont">
		     <input type="text" class="phoneInp" name="phoneNum" id="phoneNum"
					placeholder="请输入用户手机号码" />
				<button type="button" class="phoneBtn"
				onClick="goNext('<%=userId%>')">下一步</button>
		</div>
	</div>
	<%
		}else{//没有登录
			
			if(message==null){
				message = "请输入工作人员用户名和密码";
			}
				
	%>
		<p style="color: red;"><%=message%></p>
	<%
		}
	%>
</body>
</html>